package cn.iocoder.yudao.module.system.config.transcoder;

import cn.iocoder.yudao.module.system.config.a_gaojian.CkmConfig;
import cn.iocoder.yudao.module.system.dal.dataobject.a_audio_and_video.AudioAndVideoDO;
import cn.iocoder.yudao.module.system.dal.mysql.a_audio_and_video.AudioAndVideoMapper;
import cn.iocoder.yudao.module.system.service.a_ai.service.AIService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 智能提取任务处理
 */
@Component
@Slf4j
public class ExtractTask {

    @Value("${fileData.videoHost}")
    private String VIDEO_HOST;

    @Resource
    AIService aiService;

    @Resource
    private AudioAndVideoMapper audioAndVideoMapper;

    @Async("extractTaskExecutor")
    public void run(AudioAndVideoDO audioAndVideoDO) {
        log.info("智能提取任务开始执行 -->" + Thread.currentThread().getName() + " time:" + System.currentTimeMillis());
        try {
            String result = aiService.speechAsr(audioAndVideoDO.getUrl(), true, "", true);
//            String result = aiService.speechAsr("http://10.101.221.237/DL-IVRS/static/upload/video/816a48ad339ed1aec1fcadb5676a5ef5/笑谈渴饮匈奴血-裁剪音频-30-60ss.wav", true, "", true);
            log.info("智能提取结果：" + result);
            JSONObject object = JSON.parseObject(result);
            if (object.getInteger("code") == 1) {
                StringBuffer sb = new StringBuffer();
                JSONArray asrArray = object.getJSONArray("result");
                for (int i = 0; i < asrArray.size(); i++) {
                    JSONObject as = asrArray.getJSONObject(i);
                    String text = as.getString("text");
                    sb.append(text + " ");
                }
                //视频关键词抽取
                CkmConfig ckmConfig = new CkmConfig();
                List<String> keywords = ckmConfig.getKeyword(sb.toString(), 10);
                if (keywords.size() > 0) {
                    audioAndVideoDO.setKeyword(keywords.stream().collect(Collectors.joining(" ")));
                }
                //智能标记提取（关键词前2个）
                if (keywords.size() > 2) {
                    audioAndVideoDO.setMark(keywords.get(0) + " " + keywords.get(1));
                }
                audioAndVideoDO.setExtractStatus(10);
                audioAndVideoDO.setCaptions(sb.toString());
                audioAndVideoMapper.save(audioAndVideoDO);
            } else {
                audioAndVideoDO.setExtractStatus(-1);
                audioAndVideoMapper.save(audioAndVideoDO);
            }
        } catch (Exception e) {
            audioAndVideoDO.setExtractStatus(-1);
            audioAndVideoMapper.save(audioAndVideoDO);
            log.error(e.getMessage());
        }
        log.info("智能提取任务结束 -->" + Thread.currentThread().getName() + " time:" + System.currentTimeMillis());
    }

}
